package com.nbjtjc.safe.service.edu;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.edu.EduDailyExamineListMapper;
import com.nbjtjc.safe.mapper.edu.EduDailyExamineScoreMapper;
import com.nbjtjc.safe.model.edu.EduDailyExamineList;
import com.nbjtjc.safe.model.edu.EduDailyExamineScore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class EduDailyExamineListService {
	@Autowired
	  private EduDailyExamineListMapper eduDailyExamineListMapper;
	@Autowired
	EduDailyExamineScoreMapper eduDailyExamineScoreMapper;
		
	  public Result add(EduDailyExamineList eduDailyExamineList){
		        Result result=new Result();
		        if(ObjectUtil.isNull(eduDailyExamineList)
		        		||ObjectUtil.isNull(eduDailyExamineList.getRecordId())
		        		){		            
		        	result.setFalse("关键字段用户ID和考核表ID不能为空");
		            return result;
		        }
//主键为空或库中无此主键则为新建，插入数据
		        if(ObjectUtil.isNull(eduDailyExamineList.getId())|| ObjectUtil.isNull(eduDailyExamineListMapper.selectByPrimaryKey(eduDailyExamineList.getId()))){
		        	eduDailyExamineList.setCancel(0);
		        	int res=eduDailyExamineListMapper.insertSelective(eduDailyExamineList);
			        if(res==0){
			            result.setFalse("插入日常培训考核表失败");
			            return result;
			        }
			        //新建考核表分数列表
//			        res = eduDailyExamineScoreMapper.insertByExamList(eduDailyExamineList);
			        result = insertEduDailyExamineScore(eduDailyExamineList);

			        if(res==0){
			            result.setFalse("创建日常培训考核分数列表失败");
			            return result;
			        }		
		        }else
		        {
		        	int res=eduDailyExamineListMapper.updateByPrimaryKeySelective(eduDailyExamineList);
		        	 if(res==0){
		        		 result.setFalse("更新日常培训考核表失败");
				         return result;

		        	 }
		        	 //更新考核表分数列表
		        	 //先删除，后新建
		        	 res = eduDailyExamineScoreMapper.deleteByExamId(eduDailyExamineList.getId());
		        	 if(res==0){
		        		 result.setFalse("删除原考核表分数失败");
				         return result;
		        	 }
			        result = insertEduDailyExamineScore(eduDailyExamineList);
			        if(result.isSuccess()==false){
			            return result;
			        }       	 
		        }
		        result.setData(eduDailyExamineList);

		        return result;
		    }

	    private Result insertEduDailyExamineScore(EduDailyExamineList eduDailyExamineList){
	        Result result=new Result();
	    	if(CollectionUtil.isEmpty(eduDailyExamineList.getEduDailyExamineScores())){
	            result.setFalse("插入对象为空");
	        	return result;

		    }
	    	//将RequirementSurveyItem的reqInvestId都设为当前eduDailyExamineList的ID
	    	List<EduDailyExamineScore> eduDailyExamineScores =  eduDailyExamineList.getEduDailyExamineScores();
			for(EduDailyExamineScore eduDailyExamineScore: eduDailyExamineScores){
				eduDailyExamineScore.setExamineId(eduDailyExamineList.getId());
				eduDailyExamineScore.setCancel(0);
			}
	    	int res=eduDailyExamineScoreMapper.insertList(eduDailyExamineList.getEduDailyExamineScores());
	    	if(res==0){
	            result.setFalse("插入需求调查项目失败");
	            return result;
	        }
	    	result.setData(eduDailyExamineList.getEduDailyExamineScores());
	    	return result;
	    }

	  
	  public Result delete(int eduDailyExamineListId){
	      Result result=new Result();
	      if(ObjectUtil.isNull(eduDailyExamineListId)){
	          result.setFalse("删除对象不能为空");
	          return result;
	      }
	      int res = eduDailyExamineListMapper.deleteByPrimaryKey(eduDailyExamineListId);
	      if(res==0){
	    	  result.setFalse("删除失败");
		      return result;
	
	      }
	      return result;
	  }

	  public Result delete(EduDailyExamineList eduDailyExamineList){
	      Result result=new Result();
	      if(ObjectUtil.isNull(eduDailyExamineList.getId())){
	          result.setFalse("作废对象不能为空");
	          return result;
	      }
	     int  res = eduDailyExamineListMapper.deleteByPrimaryKey(eduDailyExamineList.getId());
	      if(res==0){
	   	  result.setFalse("删除考核表失败");
		      return result;
	
	     }
//	      //将考核表主表标记为
//	      eduDailyExamineList.setCancel(1);
//	      int res = eduDailyExamineListMapper.updateByPrimaryKey(eduDailyExamineList);
//	      if(res==0){
//	    	  result.setFalse("作废考核表失败");
//		      return result;
//	
//	      }
//	      //将考核分数子表标记为删除
//			  res = eduDailyExamineScoreMapper.cancelByExamId(eduDailyExamineList.getId());
//		      if(res==0){
//		    	  result.setFalse("作废考核分数失败");
//			      return result;
//		
//		      }
		  

	      return result;
	  }
	  
	  public Result find(EduDailyExamineList eduDailyExamineList) {
	      Result result = new Result();
	      PageHelper.startPage(eduDailyExamineList.getPageCurrent(), eduDailyExamineList.getPageSize());
	      List<EduDailyExamineList> eduDailyExamineLists = eduDailyExamineListMapper.search(eduDailyExamineList);
	      result.setData(new PageInfo<>(eduDailyExamineLists));

	      return result;
	  }
	  
	  public Result findDetail(EduDailyExamineList eduDailyExamineList) {
	      Result result = new Result();
	      PageHelper.startPage(eduDailyExamineList.getPageCurrent(), eduDailyExamineList.getPageSize());
	      List<EduDailyExamineList> eduDailyExamineLists = eduDailyExamineListMapper.searchDetail(eduDailyExamineList);
	      result.setData(new PageInfo<>(eduDailyExamineLists));

	      return result;
	  }
	  
//	  public Result findByExamId(EduDailyExamineList eduDailyExamineList) {
//	      Result result = new Result();
//	      PageHelper.startPage(eduDailyExamineList.getPageCurrent(), eduDailyExamineList.getPageSize());
//	      List<EduDailyExamineList> eduDailyExamineLists = eduDailyExamineListMapper.findByExamId(eduDailyExamineList.getId());
//	      result.setData(new PageInfo<>(eduDailyExamineLists));
//
//	      return result;
//	  }
	  
		public Result batchInputScores(EduDailyExamineList eduDailyExamineList) {
			// TODO Auto-generated method stub
		    Result result = new Result();
	        List<Integer> ids = new ArrayList();
	        List<Integer> scores = new ArrayList();
	        ids = eduDailyExamineList.getIds();
	        scores=eduDailyExamineList.getScores();
	        int res = 0;
	        for(int i = 0;i<ids.size();i++){
	        	EduDailyExamineScore eduDailyExamineScore = eduDailyExamineScoreMapper.selectByPrimaryKey(ids.get(i));
	        	eduDailyExamineScore.setScore(scores.get(i));
	        	res = 0;
	        	res = eduDailyExamineScoreMapper.updateByPrimaryKeySelective(eduDailyExamineScore);
	    	    if(res==0){
	    	        result.setFalse("更新考核分数失败");
	    	        return result;
	    	        }
	        }
		    result.setData(eduDailyExamineList);

			return result;
		}
}
